﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
#include <QIcon>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
    setWindowIcon(QIcon(":/pic/mine.ico"));
    ui->setupUi(this);
    this->setFixedSize(400, 300);
    ui->welcome->setText(QObject::tr("经典扫雷"));
    ui->welcome->resize(QSize(240, 60));
    ui->welcome->move(80, 80);
    ui->pushButton->move(71, 200);
    ui->exitBtn->move(235, 200);
    selectRank();
    setBtnFont();
    connect(ui->pushButton, &QPushButton::clicked, this, &MainWindow::playGame);
    connect(ui->exitBtn, &QPushButton::clicked, this, &MainWindow::close);
    connect(ui->easyRankBtn, &QAction::triggered, this, &MainWindow::setEasy);
    connect(ui->middleRankBtn, &QAction::triggered, this,
            &MainWindow::setMiddle);
    connect(ui->hardRankBtn, &QAction::triggered, this, &MainWindow::setHard);
    connect(ui->rankByUsrBtnNew,&QAction::triggered,this,&MainWindow::setUnknown);
    setMouseTracking(true);
}

MainWindow::~MainWindow() { delete ui; }

void MainWindow::playGame() {
    selectRank();
    myscene = new MyScene;
    myscene->show();
    this->hide();
    connect(myscene, &MyScene::exitWin, this, &MainWindow::show);
}

void MainWindow::selectRank() {
    switch (gameRank) {
    case 0:
        MINE_NUM = easyMine;
        GRID_NUM_HOR = easyWidth;
        GRID_NUM_VER = easyHeight;
        break;
    case 1:
        MINE_NUM = middleMine;
        GRID_NUM_HOR = middleWidth;
        GRID_NUM_VER = middleHeight;
        break;
    case 2:
        MINE_NUM = hardMine;
        GRID_NUM_HOR = hardWidth;
        GRID_NUM_VER = hardHeight;
        break;
    case 3:
        MINE_NUM = configMinNUM;
        GRID_NUM_HOR = configGridNumHor;
        GRID_NUM_VER = configGridNumVer;
        break;
    default:
        break;
    }
}

void MainWindow::setEasy() {
    gameRank = 0;
    selectRank();
    setBtnFont();
}

void MainWindow::setMiddle() {
    gameRank = 1;
    selectRank();
    setBtnFont();
}

void MainWindow::setHard() {
    gameRank = 2;
    selectRank();
    setBtnFont();
}

void MainWindow::setUnknown()
{
    gameRank = 3;
    selectRank();
    setBtnFont();
}



void MainWindow::setBtnFont() {
    QFont boldFont;
    QFont commonFont;
    boldFont.setBold(true);
    commonFont.setBold(false);
    switch (gameRank) {
    case 0:
        ui->easyRankBtn->setFont(boldFont);
        ui->middleRankBtn->setFont(commonFont);
        ui->hardRankBtn->setFont(commonFont);
        ui->rankByUsrBtnNew->setFont(commonFont);
        break;
    case 1:
        ui->easyRankBtn->setFont(commonFont);
        ui->middleRankBtn->setFont(boldFont);
        ui->hardRankBtn->setFont(commonFont);
        ui->rankByUsrBtnNew->setFont(commonFont);
        break;
    case 2:
        ui->easyRankBtn->setFont(commonFont);
        ui->middleRankBtn->setFont(commonFont);
        ui->hardRankBtn->setFont(boldFont);
        ui->rankByUsrBtnNew->setFont(commonFont);
        break;
    case 3:
        ui->easyRankBtn->setFont(commonFont);
        ui->middleRankBtn->setFont(commonFont);
        ui->hardRankBtn->setFont(commonFont);
        ui->rankByUsrBtnNew->setFont(boldFont);
        break;
    default:
        break;
    }
}


